<!--
 * @Description: 日志管理
 * @Version: 1.0
 * @Author: yxx
 * @Date: 2023-09-25 15:31:11
 * @LastEditors: yxx
 * @LastEditTime: 2024-12-09 16:32:31
-->
<template>
  <div class="log-manage-container">
    <el-card>
      <el-form :inline="true" :model="formInline" class="fliter-form-inline">
        <el-form-item label="操作人员">
          <el-input v-model="formInline.name" placeholder="请输入" clearable></el-input>
        </el-form-item>
        <el-form-item label="操作时间">
          <el-date-picker
            v-model="formInline.time"
            type="datetimerange"
            value-format="YYYY-MM-DD HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getTableData">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="table">
      <el-table :data="tableData" tooltip-effect="dark" style="width: 100%; margin-top: 20px" border>
        <el-table-column type="index" label="序号" width="60"></el-table-column>
        <el-table-column prop="id" label="日志编号" width="120"> </el-table-column>
        <el-table-column prop="account" label="登录用户" width="120"> </el-table-column>
        <el-table-column prop="ip" label="ip地址" width="160"> </el-table-column>
        <el-table-column prop="browser" label="浏览器类型" width="120"> </el-table-column>
        <el-table-column prop="os" label="操作系统" width="300"> </el-table-column>
        <el-table-column prop="result" label="操作状态" width="120">
          <template #default="scope">
            <div :style="{ color: scope.row.result === 1 ? '#67C23A' : '#F56C6C' }">
              {{ scope.row.result === 1 ? "成功" : "失败" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="loginTime" label="登录日期" width="220"></el-table-column>
        <el-table-column prop="msg" label="登录信息"></el-table-column>
      </el-table>
    </el-card>
    <el-pagination
      background
      layout="->, sizes, prev, pager, next"
      :page-sizes="[10, 20, 50, 100]"
      :current-page="page.pageIndex"
      :page-size="page.pageSize"
      :total="page.totalSize"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue"
import { getLoginLogPageListApi } from "@/api/modules/system"
import { SysRes } from "@/api/interface/system/response"

interface FormData {
  name: string
  time: string[]
}
const formInline = ref<FormData>({
  name: "",
  time: []
})
const tableData = ref<SysRes.LoginLogPageList[]>([])
const page = ref<{ [key: string]: number }>({
  pageIndex: 1,
  pageSize: 10,
  totalSize: 0
})

/** 获取 表格数据 */
const getTableData = async () => {
  const parmas = {
    uid: formInline.value.name,
    startTime: formInline.value.time.length === 0 ? "" : formInline.value.time[0],
    endTime: formInline.value.time.length === 0 ? "" : formInline.value.time[1],
    pageIndex: page.value.pageIndex,
    pageSize: page.value.pageSize
  }
  const { data } = await getLoginLogPageListApi(parmas)
  tableData.value = data.result
  page.value.totalSize = data.totalCount
}
/** 按钮 重置 */
const handleReset = () => {
  formInline.value = {
    name: "",
    time: []
  }
  getTableData()
}
/** 分页 显示数量改变 */
const handleSizeChange = (val: number) => {
  page.value.pageIndex = 1
  page.value.pageSize = val
  getTableData()
}
/** 分页 当前页数改变 */
const handleCurrentChange = (val: number) => {
  page.value.pageIndex = val
  getTableData()
}

onMounted(() => {
  getTableData()
})
</script>

<style scoped lang="scss">
.log-manage-container {
  .table {
    margin-top: 10px;
  }
}
</style>
